import os
import shutil
from pathlib import Path

def clean_project():
    """清理项目缓存和临时文件"""
    # 获取项目根目录
    root_dir = Path(__file__).parent.parent
    
    # 需要清理的文件和目录
    patterns_to_clean = [
        '**/__pycache__',
        '**/*.pyc',
        '**/*.pyo',
        '**/*.pyd',
        '**/instance/*',
        '**/logs/*',
        '.qiniu_pythonsdk_hostscache.json',
        '**/temp/*',
        '**/dist/*',
        '**/build/*',
        '**/*.egg-info'
    ]
    
    # 需要保留的空目录
    dirs_to_keep = ['instance', 'logs', 'temp']
    
    try:
        # 清理文件和目录
        for pattern in patterns_to_clean:
            for path in root_dir.glob(pattern):
                if path.is_file():
                    path.unlink()
                    print(f"已删除文件: {path}")
                elif path.is_dir():
                    shutil.rmtree(path)
                    print(f"已删除目录: {path}")
        
        # 重新创建需要保留的空目录
        for dir_name in dirs_to_keep:
            dir_path = root_dir / dir_name
            dir_path.mkdir(exist_ok=True)
            print(f"已创建目录: {dir_path}")
            
        print("清理完成！")
        
    except Exception as e:
        print(f"清理过程中出错: {str(e)}")

if __name__ == '__main__':
    clean_project() 